package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.constant.SafeguardCacheConstant;
import com.itheima.sfbx.insurance.dto.SafeguardVO;
import com.itheima.sfbx.insurance.enums.SafeguardEnum;
import com.itheima.sfbx.insurance.mapper.SafeguardMapper;
import com.itheima.sfbx.insurance.pojo.CategorySafeguard;
import com.itheima.sfbx.insurance.pojo.PlanSafeguard;
import com.itheima.sfbx.insurance.pojo.Safeguard;
import com.itheima.sfbx.insurance.service.ICategorySafeguardService;
import com.itheima.sfbx.insurance.service.IPlanSafeguardService;
import com.itheima.sfbx.insurance.service.ISafeguardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description：保障项服务实现类
 */
@Slf4j
@Service
public class SafeguardServiceImpl extends ServiceImpl<SafeguardMapper, Safeguard> implements ISafeguardService {

    private LambdaQueryWrapper<Safeguard> queryWrapper(SafeguardVO safeguardVO){
        LambdaQueryWrapper<Safeguard> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(safeguardVO.getSafeguardKey()), Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .like(StrUtil.isNotBlank(safeguardVO.getSafeguardKeyName()), Safeguard::getSafeguardKeyName, safeguardVO.getSafeguardKeyName())
                .eq(StrUtil.isNotBlank(safeguardVO.getDataState()), Safeguard::getDataState, safeguardVO.getDataState());
        return lqw;
    }

    @Override
    @Cacheable(value = SafeguardCacheConstant.PAGE,key = "#pageNum+'-'+#pageSize+'-'+#safeguardVO.hashCode()")
    public Page<SafeguardVO> findPage(SafeguardVO safeguardVO, int pageNum, int pageSize) {
        LambdaQueryWrapper<Safeguard> lqw = queryWrapper(safeguardVO);
        Page<Safeguard> page = this.page(new Page<>(pageNum, pageSize), lqw);
        Page<SafeguardVO> voPage = BeanConv.toPage(page, SafeguardVO.class);
        return voPage;
    }

    @Override
    public SafeguardVO findById(String safeguardId) {
        //TODO
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SafeguardVO save(SafeguardVO safeguardVO) {
        Integer count = lambdaQuery()
                .eq(Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .eq(Safeguard::getDataState, safeguardVO.getDataState())
                .count();
        if (count>0){
            throw new ProjectException(SafeguardEnum.SAVE_FAIL);
        }
        Safeguard bean = BeanConv.toBean(safeguardVO, Safeguard.class);
        this.save(bean);
        return BeanConv.toBean(bean, SafeguardVO.class);
    }

    @Autowired
    private ICategorySafeguardService categorySafeguardService;
    @Autowired
    private IPlanSafeguardService planSafeguardService;
    @Override
    @Transactional
    public Boolean update(SafeguardVO safeguardVO) {
        Safeguard safeguard = this.getById(safeguardVO.getId());
        if (safeguard==null){
            log.error("数据不存在:{}",safeguardVO.getId());
            throw new ProjectException(SafeguardEnum.UPDATE_FAIL);
        }
        Integer count = lambdaQuery().eq(Safeguard::getSafeguardKey, safeguardVO.getSafeguardKey())
                .ne(Safeguard::getId, safeguardVO.getId())
                .count();
        if (count>0){
            log.error("Key已经存在:{}",safeguardVO.getSafeguardKey());
            throw new ProjectException(SafeguardEnum.UPDATE_FAIL);
        }
        Integer count1 = categorySafeguardService.lambdaQuery().eq(CategorySafeguard::getSafeguardKey, safeguardVO.getSafeguardKey()).count();
        Integer count2 = planSafeguardService.lambdaQuery().eq(PlanSafeguard::getSafeguardKey, safeguardVO.getSafeguardKey()).count();
        if (count1>0||count2>0){
            log.error("有关联:{}",safeguardVO.getSafeguardKey());
            throw new ProjectException(SafeguardEnum.UPDATE_FAIL);
        }
        return updateById(BeanConv.toBean(safeguardVO,Safeguard.class));
    }

    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        //TODO
        return null;
    }

    @Override
    public List<SafeguardVO> findList(SafeguardVO safeguardVO) {
        LambdaQueryWrapper<Safeguard> lqw = queryWrapper(safeguardVO);
        List<Safeguard> list = this.list(lqw);
        return BeanConv.toBeanList(list,SafeguardVO.class);
    }


    @Override
    public List<SafeguardVO> findShowPageItemByKey(List<String> safeguardKeyList) {
        //TODO
        return null;
    }

    @Override
    public SafeguardVO findBySafeguardKey(String safeguardKey) {
        //TODO
        return null;
    }
}
